package com.wtwd.campus.service.impl;

import com.wtwd.campus.common.Constants;
import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.common.RespEntity;
import com.wtwd.campus.dao.DeviceInfoMapper;
import com.wtwd.campus.dao.SosCallMapper;
import com.wtwd.campus.entity.DeviceInfo;
import com.wtwd.campus.entity.DeviceSOSList;
import com.wtwd.campus.service.SosCallService;
import com.wtwd.campus.utils.CommandNumberUtil;
import com.wtwd.campus.utils.CommandSendUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mjy
 * @date 2021/07/20
 * @description
 */
@Service
public class SosCallServiceImpl implements SosCallService {

    @Autowired
    private CommandSendUtils commandSendUtils;
    @Autowired
    private SosCallMapper sosCallMapper;
    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public RespEntity setSosPhone(Integer userId, List<DeviceSOSList> deviceSOSLists) {
        boolean online = false;
        String imei = "";
        ArrayList<DeviceSOSList> bathAddDeviceSos = new ArrayList<>();//新增集合
        ArrayList<DeviceSOSList> bathUpdateDeviceSos = new ArrayList<>();//修改集合
        ArrayList<DeviceSOSList> bathDelDeviceSos = new ArrayList<>();//删除集合
        String commandNumber = CommandNumberUtil.getCurTime();
        //构建发送指令存储集合
        List<DeviceSOSList> sendSosList = new ArrayList<>();
        //这里前端传过来的数据不会为空，如果手机号，和姓名都为空，也需要提交其他参数过来
        for (DeviceSOSList deviceSOSList : deviceSOSLists) {
             if (StringUtils.isBlank(imei)){
                 DeviceInfo deviceInfoByDeviceId = deviceInfoMapper.getDeviceInfoByDeviceId(deviceSOSList.getDeviceId());
                 if (deviceInfoByDeviceId!=null){
                     imei=deviceInfoByDeviceId.getImei();
                     online=commandSendUtils.deviceOnline(imei);
                 }
             }
            //删除信息
            if (StringUtils.isBlank(deviceSOSList.getSosPhone())) {
                //手机号码为空----将姓名也置空，相当于删除
                deviceSOSList.setStatus(Constants.CLASSES_PATTERN_STAY_DELETE);
                deviceSOSList.setSosName("");
                deviceSOSList.setCommandNumber(commandNumber);
                bathDelDeviceSos.add(deviceSOSList);
                sendSosList.add(deviceSOSList);
            } else {//鉴别是新增数据还是修改数据
                DeviceSOSList deviceSOSList1 = sosCallMapper.getSosCallByDeviceIdAndSequence(deviceSOSList.getDeviceId(), deviceSOSList.getSequence());
                if (deviceSOSList1 != null) {//记录为修改数据
                    deviceSOSList.setStatus(Constants.CLASSES_PATTERN_STAY_UPDATE);
                    deviceSOSList.setCommandNumber(commandNumber);
                    bathUpdateDeviceSos.add(deviceSOSList);
                    sendSosList.add(deviceSOSList);
                } else {//记录为新增数据
                    deviceSOSList.setStatus(Constants.CLASSES_PATTERN_STAY_ADD);
                    deviceSOSList.setCommandNumber(commandNumber);
                    bathAddDeviceSos.add(deviceSOSList);
                    sendSosList.add(deviceSOSList);
                }
            }
        }
        //排序
        List<DeviceSOSList> sosLists = sendSosList.stream().sorted(Comparator.comparing(DeviceSOSList::getSequence)).collect(Collectors.toList());
        //发送离线指令
        commandSendUtils.setAllSosCall(sosLists, imei, commandNumber, online, userId, false);
        if (bathAddDeviceSos != null&&bathAddDeviceSos.size()>0) {
            sosCallMapper.bathAddDeviceSosCall(bathAddDeviceSos);
        }
        if (bathUpdateDeviceSos != null&&bathUpdateDeviceSos.size()>0) {
            sosCallMapper.bathUpdateDeviceSosCallStateAndCommandNumber(bathUpdateDeviceSos, Constants.CLASSES_PATTERN_STAY_UPDATE,commandNumber);
        }
        if (bathDelDeviceSos != null&&bathDelDeviceSos.size()>0) {
            sosCallMapper.bathUpdateDeviceSosCallStateAndCommandNumber(bathDelDeviceSos, Constants.CLASSES_PATTERN_STAY_DELETE,commandNumber);
        }
        return new RespEntity(RespCode.SUCCESS);
    }

    @Override
    public List<DeviceSOSList> getSosCallService(Integer deviceId) {
        return sosCallMapper.getSosCallService(deviceId);
    }

    @Override
    public Integer deletePhoneByDeviceId(Integer deviceId) {
        return sosCallMapper.deletePhoneByDeviceId(deviceId);
    }

    @Override
    public Integer bathAddDeviceSosCall(List<DeviceSOSList> deviceSOSLists) {
        return sosCallMapper.bathAddDeviceSosCall(deviceSOSLists);
    }


}
